$(function () {
    var form = layui.form

    form.verify({
        same: function (value) {
            var uname = $('.layui-form input[name=password]').val()
            if (value !== uname) {
                return '两次输入的密码不一样'
            }
        },
        username: function (value, item) { //value：表单的值、item：表单的DOM对象
            if (!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
                return '用户名不能有特殊字符';
            }
            if (/(^\_)|(\__)|(\_+$)/.test(value)) {
                return '用户名首尾不能出现下划线\'_\'';
            }
        },
        //我们既支持上述函数式的方式，也支持下述数组的形式
        //数组的两个值分别代表：[正则匹配、匹配不符时的提示文字]
        pass: [
            /^[\S]{6,12}$/, '密码必须6到12位，且不能出现空格'
        ]
    })
})
var laypage = layui.laypage;

var data = {
    pagenum: 1,
    pagesize: 3,
};

//*******************1.用户列表展示(渲染页面)********************* */
function renderUser() {
    $.ajax({
        url: 'http://localhost:8888/admin/users',
        data: data,
        success: function (res) {
            if (res.status == 0) {
                var html = template('tpl-list', res);
                $('tbody').html(html);


                createPage(res.total);
            }
        },
        // headers: {
        //     'Authorization': "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiaWQiOjEsImlhdCI6MTU5OTUyNjEwMywiZXhwIjoxNTk5NTI5NzAzfQ.ZGV7c2JfVhtylimi_LbAFm2HD_dt1n-h0J-eFWhxdzM",
        // },
        headers: {
            Authorization: localStorage.getItem('token')
        },
    })
}
renderUser();

//*******************分页信息********************* */
function createPage(total) {
    laypage.render({
        elem: 'page', //注意，这里的 test1 是 ID，不用加 # 号
        curr: data.pagenum, //显示当前页,实际中应该和data.pagenum一样
        layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
        limit: data.pagesize, //一行显示几条数据，默认是10条
        count: total, //数据总数，从服务端得到 
        limits: [3, 5, 10, 20], //可选显示几条数据
        jump: function (obj, first) {
            //页面跳转时用的数据
            if (first == undefined) {
                data.pagenum = obj.curr;
                data.pagesize = obj.limit;
                renderUser();
            }
        }
    });
}
//************************删除用户************************** */
$('tbody').on('click', '#del', function (e) {
    var id = $(this).data('id');
    layer.confirm('确定要删除用户吗?', {
        icon: 3,
        title: '提示'
    }, function (index) {
        //do something
        $.ajax({
            type: 'DELETE',
            url: 'http://localhost:8888/admin/users/' + id,
            success: function (res) {
                layer.msg(res.message);
                if (res.status == 0) {
                    renderUser();
                }
            },
            // headers: {
            //     'Authorization': "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiaWQiOjEsImlhdCI6MTU5OTUyNjEwMywiZXhwIjoxNTk5NTI5NzAzfQ.ZGV7c2JfVhtylimi_LbAFm2HD_dt1n-h0J-eFWhxdzM",
            // },
            headers: {
                Authorization: localStorage.getItem('token')
            },
        })
        layer.close(index);
    });
})
//********************重置密码(出弹框)************************ */
$('tbody').on('click', '#res-pwd', function (e) {
    var id = $(this).data('id');
    var index = layer.open({
        type: 1,
        title: '重置密码',
        area: ['500px', '250px'],
        content: $('#pwd').html()
    });
    // *****************重置密码(提交信息)*************************
    $('body').on('submit', '#reset-pwd', function (e) {
        e.preventDefault();
        var data = $(this).serialize();
        // console.log(data);
        $.ajax({
            type: 'PUT',
            url: 'http://localhost:8888/admin/users/' + id,
            data: data,
            success: function (res) {
                layer.msg(res.message);
                if (res.status == 0) {
                    renderUser();
                    layer.close(index);
                }
            },
            // headers: {
            //     'Authorization': "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiaWQiOjEsImlhdCI6MTU5OTUyNjEwMywiZXhwIjoxNTk5NTI5NzAzfQ.ZGV7c2JfVhtylimi_LbAFm2HD_dt1n-h0J-eFWhxdzM",
            // },
            headers: {
                Authorization: localStorage.getItem('token')
            },
        })
    })
})


// 重置密码信息验证
var form = layui.form;
form.verify({
    //两次密码要一致
    same: function (val) {
        var surePwd = $('input[name="password"]').val();
        if (surePwd !== val) {
            return '两次密码不一致'
        }
    }
})